package proxy.util;

import org.apache.commons.codec.binary.Base64;

import proxy.Configure;
import proxy.model.User;

public class AuthenticationUtil {

	public static User decodeBasicAuthentication(String encode){
		String code = new String(Base64.decodeBase64(encode.substring(6)));
		int position = code.indexOf(":");
		User user = null;
		if (position != -1){
			user = new User();
			user.username = code.substring(0, position);
			user.password = code.substring(position + 1);
		}
		return user;
	}
	
	public static boolean authentication(String encode){
		User user = decodeBasicAuthentication(encode);
		boolean ret = false;
		if (user != null && Configure.getInstance().users.contains(user)){
			ret = true;
		}
		return ret;
	}
}
